import giantnengjiangDetailsZj from '@/components/giantnengjiangDetailsZj.vue'
import share from '@/components/i-share-popupQq.vue'
const Clipboard = require('@/utils/clipboard.js')
export default {
  components: {
    share,
    giantnengjiangDetailsZj
  },
  data() {
    return {
      bannar: [],
      stowUnfold: true, //收起展开
      carllist: {}, //车子信息
      stiplist: [], //店铺信息
      carid: '', //车子id
      xedColorList: [], //颜色list
      colorpopup: false, //颜色弹框

      success: false, //
      sharePopupShow: false, // 分享弹窗
      lon: 0,
      lat: 0,
      topback: false, //是否返回键-是否显示打开app
      Invitationcode: '',
      showPage: false
    }
  },
  created() {},
  onLoad(caridxix) {
    this.carid = caridxix.goodid
    this.topback = caridxix.topback

    this.lon = this.$store.state.lon
    this.lat = this.$store.state.lat
    this.GetGoodsCarSourceDetails()

    // 好友的邀请码
    // if (this.topback) {
    if (caridxix.code !== '') {
      this.Invitationcode = caridxix.code //接收邀请码
      //邀请码存储-缓存
      uni.setStorageSync('invitationcode', this.Invitationcode)
    } else {
      this.Invitationcode = '' //没有登录
    }
    // }

    // #ifdef H5
    this.$nextTick(() => {
      // H5复制文本
      const btnCopy = new Clipboard('.copycss')
      btnCopy.on('success', e => {
        uni.showToast({
          icon: 'none',
          title: '复制成功'
        })
      })

      // 打开APP当前页
      var data = {
        url: encodeURIComponent('/pages_commodity/giantnengjiangDetails/giantnengjiangDetails?goodid=' + this.carid +
          '&code=' +
          this.Invitationcode),
      }
      new XInstall({
        appKey: 'pnrh2k6',
        onready: function() {
          // shareinstall已成功回调
          var m = this,
            button = document.getElementById('openapps')
          button.style.visibility = 'visible'
          // 用户点击某个按钮时(假定按钮id为downloadButton)，安装app
          button.onclick = function() {
            m.wakeupOrInstall()
          }
        }
      }, data)
    })
    // #endif
  },
  onPageScroll(e) {
    // console.log(e.scrollTop)
    if (e.scrollTop > this.Topdistance) {
      this.isFixedTop = true
      this.success = false
    } else {
      this.isFixedTop = false
      this.success = false
    }
  },
  onShow() {
    this.$nextTick(() => {
      // #ifdef APP-PLUS
      let webview = this.$mp.page.$getAppWebview()
      webview.setTitleNViewButtonStyle(0, {
        text: '',
        width: '30px',
        color: '#3d3d3d',
      })
      // #endif
      // #ifdef H5
      let view = document.querySelectorAll('.uni-page-head-ft .uni-page-head-btn')
      console.log('view', view)
      view[1].childNodes[0].innerHTML = ''
      view[1].style.width = '0'

      if (this.topback) {
        let view = document.querySelectorAll('.uni-page-head-ft .uni-page-head-btn')
        console.log('view', view)
        view[0].childNodes[0].innerHTML = ''
        view[0].style.width = '0'
      }
      // #endif
    })
  },
  methods: {
    // 点击导航栏右侧
    onNavigationBarButtonTap(e) {
      // index=0更多,index=1分享
      if (e.index === 0) {
        this.success = !this.success
      } else {
        this.sharePopupShow = true
      }
    },
    //展开收起
    shtfclick() {
      this.stowUnfold = !this.stowUnfold
    },

    //详情
    async GetGoodsCarSourceDetails(type) {
      try {
        const res = await this.$api.commodity.GetGoodsCarSourceDetails({
          id: this.carid,
          lat: this.lat,
          lon: this.lon
        })
        this.showPage = true
        res.data.valid_Time = res.data.valid_Time.split('T')[0]

        this.carllist = res.data
        this.stiplist = res.data.storeDetails
        this.bannar = res.data.car_ImageList
        // 压缩图片
        // this.bannar.forEach(item => {
        //   item.image += '!clientdetail'
        // })
        this.xedColorList = this.filtercolor(res.data.colourList) //颜色
        this.xedColorList.forEach(item => {
          item.chekd = false
        })
        console.log(this.xedColorList)
      } catch (err) {}
    },

    //分割颜色
    filtercolor(data) {
      data.forEach(item => {
        var arr = item.inColour.colorCoding.split('/')
        item.inColour.neiColor = arr
      })
      return data
    },
    // 颜色弹框
    colorClick() {
      this.colorpopup = true
    },
    //查看配置
    lookpeiclick() {
      uni.navigateTo({
        url: '/pages_common/productDcarConfigs/productDcarConfigs?carsList=' + encodeURIComponent(JSON.stringify(
          this.carllist))
      })
    },
    GetTop() {
      //获取元素距离顶部的距离
      var _this = this
      this.success = false
      uni.getSystemInfo({
        success: resu => {
          // console.log(resu)
          const query = uni.createSelectorQuery()
          query.select('#box').boundingClientRect()
          query.selectViewport().scrollOffset()
          query.exec(function(res) {
            _this.Topdistance = res[0].top
            // console.log(res)
          })
        },
        fail: res => {}
      })
    },

    bigimgList(index) {
      // 预览图片多张
      // previewImg(index) {
      let imgsArray = []
      for (let i = 0; i < this.bannar.length; i++) {
        if (this.bannar[i].image !== '') {
          imgsArray.push(this.bannar[i].image)
        }
      }
      // #ifdef MP
      uni.previewImage({
        current: index,
        urls: imgsArray,
        indicator: 'number',
        loop: true
      })
      // #endif
      // #ifndef MP
      uni.previewImage({
        current: index,
        urls: imgsArray,
        indicator: 'number',
        loop: true
      })
      // #endif
    },
    // 询比价再购
    comparison() {
      let list = {
        brand_Id: this.carllist.brand_Id,
        brand_Name: this.carllist.brand_Name,
        car_Type_Id: this.carllist.car_Type_Id,
        car_Type_Name: this.carllist.car_Type_Name,
        configuration_Id: this.carllist.carid,
        configuration_Name: this.carllist.configuration_Name,
        car_Price: this.carllist.factory_Prices, //厂家指导价
        colour: this.carllist.colourList,
        type: 1
      }
      uni.navigateTo({
        url: '/price_Comparison/input_info/input_info?list=' + JSON.stringify(list)
      })
    },
    // 免费贷款测算
    Calculation() {
      let list = {
        bid: this.carllist.brand_Id,
        brand: this.carllist.brand_Name,

        car_Type_Id: this.carllist.car_Type_Id,
        car_Type_Name: this.carllist.car_Type_Name,

        carid: this.carllist.carid,
        configuration_Name: this.carllist.configuration_Name,

        type: 110
      }
      uni.navigateTo({
        url: '/pages_order/carPurchaseCalculation/carPurchaseCalculation?list=' + JSON.stringify(list)
      })
    },
    // 进入店铺
    stortClick(type) {
      uni.navigateTo({
        url: '/pages_store/newCarStore/index/index?storeid=' + type.store_Id
      })
    },
    xqshare() {
      this.success = false
      this.sharePopupShow = true
    },
    //分享微信好友
    handleShare(type) {

      // 登录成功之后返回登录信息内取到邀请码
      const value = uni.getStorageSync('Ztc_C_USER_INFO')
      var incode = ''
      if (value) {
        incode = value.invitecode
      }

      this.sharePopupShow = false
      let brandname = this.carllist.car_Type_Name + this.carllist.configuration_Name
      // 线上
      let url =
        'https://ztch5.yzsheng.com/index.html#/pages_commodity/giantnengjiangDetails/giantnengjiangDetails?goodid=' +
        this.carllist.id + '&topback=true' + '&code=' + incode
      // 测试
      // let url =
      //   'https://clientv2.yzsheng.com/index.html#/pages_commodity/giantnengjiangDetails/giantnengjiangDetails?goodid=' +
      //   this.carllist.id + '&topback=true' + '&code=' + incode
      let firdurl = '/pages_commodity/giantnengjiangDetails/giantnengjiangDetails?goodid=' + this.carllist.id
      if (type === 'WXSceneSession' || type === 'WXSenceTimeline') {
        uni.share({
          provider: 'weixin',
          scene: type,
          type: 0,
          summary: brandname,
          title: this.carllist.brand_Name,
          href: url,
          imageUrl: this.bannar[0].image,
          success: function(res) {

          },
          fail: function(err) {

            uni.showToast({
              title: '分享失败'
            })
          }
        })
      }
      if (type === 'qq') {
        uni.share({
          provider: 'qq',
          scene: 'WXSceneSession',
          type: 1,
          summary: brandname,
          title: this.carllist.brand_Name,
          href: url,
          imageUrl: this.bannar[0].image,
          success: function(res) {},
          fail: function(err) {
            uni.showToast({
              title: '分享失败'
            })
          }
        })
      }
      if (type === 'goodFriends') {
        uni.navigateTo({
          url: '/pages_buy_index/myfriend/myfriend?img=' + encodeURIComponent(this.bannar[0].image) + '&title=' +
            brandname + '&url=' + firdurl
        })
      }
      if (type === 'iconlianjie') {
        uni.setClipboardData({
          data: url,
          success: function() {
            uni.showToast({
              title: '复制成功',
              icon: 'none'
            })
          }
        })
      }

    },
  }
}
